<template>
  <div class="substation-pie-chart">
    <div ref="chartRef" class="chart-container"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import request from "@/utils/request";
export default {
  name: "SubstationPieChart",
  mounted() {
    request({
      url: "/zk/substation/substation-count-by-type",
      method: "get",
    }).then((res) => {
      console.log("result", res);
      this.initChart(res.data);
    });
  },
  methods: {
    initChart(params) {
      const chartDom = this.$refs.chartRef;
      const myChart = echarts.init(chartDom);
      const option = {
        title: {
          text: "",
          left: "center",
          textStyle: {
            color: "#FFFFFF",
          },
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b}: {c} ({d}%)",
          textStyle: {
            color: "#FFFFFF",
          },
        },
        legend: {
          orient: "vertical",
          left: "left",
          data: params.map((item) => item.type),
          textStyle: {
            color: "#FFFFFF",
          },
        },
        color: ["#4A90E2", "#50E3C2", "#7986CB", "#4DB6AC", "#64B5F6"], // Use cooler colors for the pie chart
        series: [
          {
            name: "Data Sources",
            type: "pie",
            radius: "50%",
            data: params.map((item) => ({
              value: item.value,
              name: item.type,
            })),
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
            label: {
              color: "#FFFFFF",
            },
            labelLine: {
              lineStyle: {
                color: "#FFFFFF",
              },
            },
          },
        ],
      };
      myChart.setOption(option);
    },
  },
};
</script>

<style lang="scss" scoped>
.substation-pie-chart {
  width: 100%;
  height: 100%;

  .chart-container {
    width: 100%;
    height: 243px;
  }
}
</style>
